/**
 * @Author: LiMeng
 * @Date: 2017/3/14
 */
var add_user_func = function () {
    var $departmentTree = $('#department_tree');
    var $selectedTreeNode = $departmentTree.tree('getSelected');
    var deptId;
    //未选择时获取根节点
    if ($selectedTreeNode === null) {
        deptId = $departmentTree.tree('getRoot').id;
    }else {
        deptId = $selectedTreeNode.id;
    }
    var index = layer.open({
        type: 2,
        title: '添加用户',
        area: ['1150px', '60%'],
        content: ROOT_PATH + '/permission/user/add.shtml?deptId=' + deptId
    });
};
var edit_user_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用户！');
        return false;
    }
    var $departmentTree = $('#department_tree');
    var $selectedTreeNode = $departmentTree.tree('getSelected');
    var deptId;
    //未选择时获取根节点
    if ($selectedTreeNode === null) {
        deptId = $departmentTree.tree('getRoot').id;
    }else {
        deptId = $selectedTreeNode.id;
    }
    var index = layer.open({
        type: 2,
        title: '编辑用户',
        area: ['1150px', '60%'],
        content: ROOT_PATH + '/permission/user/edit.shtml?id=' + $selected[0].id + "&deptId=" + deptId
    });
};
var delete_user_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用户！');
        return false;
    }
    layer.confirm('是否确定删除？', function () {
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/permission/user/delete.shtml',
            data: {id: $selected[0].id},
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $('#dg1').datagrid('load');
                        layer.closeAll(); //先得到当前iframe层的索引
                    });
                } else {
                    layer.msg(result.message);
                }
            }
        });
    });
};
var reset_user_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用户！');
        return false;
    }
    layer.confirm('是否确定重置该用户的密码？', function () {
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/permission/user/reset.shtml',
            data: {name: $selected[0].name},
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $('#dg1').datagrid('load');
                        layer.closeAll(); //先得到当前iframe层的索引
                    });
                } else {
                    layer.msg(result.message);
                }
            }
        });
    });
};
var move_user_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个用户！');
        return false;
    }
    var index = layer.open({
        type: 2,
        title: '移动用户',
        area: ['600px', '400px'],
        content: ROOT_PATH + '/permission/user/move.shtml?id=' + $selected[0].id
    });
};
$(function () {
    //初始化组织结构树
    $('#department_tree').tree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        lines:true,
        onSelect: function (node) {
            //重新按部门加载datagrid
            $('#dg1').datagrid('load', {
                deptCode: node.attributes.code
            });
        },
        onLoadSuccess: function (node, data) {
            var deptId = data[0].id;
            var deptCode = data[0].attributes.code;
            //初始化加载datagrid
            $('#dg1').datagrid({
                method: 'post',
                url: ROOT_PATH + '/permission/user/list.shtml',
                queryParams: {
                    sort: 'addTime',
                    order: 'desc',
                    deptCode: deptCode
                },
                toolbar:'#tb1',
                //idField: 'id',
                fitColumns: true,
                pagination: true,
                rownumbers: true,
                sortOrder: true,
                striped: true,
                fit: true,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'code', title: '用户编号', width: 100, sortable: true},
                    {field: 'name', title: '用户名', width: 80, sortable: true},
                    {field: 'realName', title: '姓名', width: 80, sortable: true},
                    {
                        field: 'deptName',
                        title: '所属部门',
                        width: 80,
                        // sortable: true,
                        formatter: function (value, row, index) {
                            var name = '';
                            if (row.userDepartmentList && row.userDepartmentList.length > 0) {
                                for (var index in row.userDepartmentList){
                                    if (row.userDepartmentList[index].deptName != null){
                                        name += ',' + row.userDepartmentList[index].deptName;
                                    }
                                }
                                return name.substring(1);
                            } else {
                                return '';
                            }
                        }
                    },
                    // {field: 'postId', title: '岗位ID', width: 80},
                    {field:'postName',title:'岗位',width:80},
                    {
                        field: 'useState',
                        title: '启用状态',
                        width: 60,
                        sortable: true,
                        formatter: function (value, row, index) {
                            if (value === 1) {
                                return '已启用';
                            } else {
                                return '未启用';
                            }
                        }
                    },
                    {field: 'addUserId', title: '添加用户ID', width: 80, hidden: true},
                    {
                        field: 'addUser',
                        title: '添加人',
                        width: 80,
                        hidden: true,
                        formatter: function (value, row, index) {
                            if (value && value.realName) {
                                return value.realName;
                            } else {
                                return '';
                            }
                        }
                    },
                    {field: 'addTime', title: '添加时间', width: 100, sortable: true},
                    {field: 'updateUserId', title: '修改用户ID', width: 80, hidden: true},
                    {
                        field: 'updateUser',
                        title: '修改人',
                        width: 80,
                        hidden: true,
                        formatter: function (value, row, index) {
                            if (value && value.realName) {
                                return value.realName;
                            } else {
                                return '';
                            }
                        }
                    },
                    {field: 'updateTime', title: '修改时间', width: 100, sortable: true},
                    {field: 'remarks', title: '备注', width: 100}
                ]]
            });
        }
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('load', $('#tb1').find(':input').serializeJson());
    });
    $('#btn_user_add').on('click', function () {
        add_user_func();
    });
    $('#btn_user_edit').on('click', function () {
        edit_user_func();
    });
    $('#btn_user_delete').on('click', function () {
        delete_user_func();
    });
    $('#btn_user_reset').on('click', function () {
        reset_user_func();
    });
    $('#btn_user_move').on('click', function () {
        move_user_func();
    });
});